(function () {
    z._cdn = null;
    z.cdn = function (opts) {
        opts = opts || {};
        /*if ( ! opts) {
            return;
        }*/
        /*if (opts.cdn_path) {
            this.cdn_path = opts.cdn_path;
        } else {
            this.cdn_path = 'upload';
        }*/
        if (opts.current_path) {
            this.current_path = opts.current_path;
        } else {
            this.current_path = '';
        }
        var othis = this;
        //this.action_url = z.site_url('m=cdn&a=explorer' + this.cdn_path);
        if ( ! z._cdn) {
            z._cdn = {};
            var c = z._cdn;
            
            c.cdn_wrap = document.createElement("div");
            
            c.cdn_nav = document.createElement('div');
            c.cdn_path = document.createElement('span');
            c.input_new_folder = document.createElement('input');
            c.bt_new_folder = document.createElement('a');
            
            c.explorer = document.createElement('div');
            c.file_info = document.createElement('div');
            
            c.cdn_nav.appendChild(c.cdn_path);
            c.cdn_nav.appendChild(c.input_new_folder);
            c.cdn_nav.appendChild(c.bt_new_folder);
            c.cdn_path.innerHTML = '/';
            
            c.input_new_folder.type = "text";
            c.input_new_folder.className = "text";
            c.input_new_folder.value = "";
            
            c.bt_new_folder.href = "javascript:;";
            c.bt_new_folder.innerHTML = "New Folder";
            c.bt_new_folder.className = "bt";
            c.bt_new_folder.onclick = function () {
                othis.new_folder();
            };
            
            
            //this.input_upload_file = input_file;
            
            c.cdn_wrap.appendChild(c.cdn_nav);
            c.cdn_wrap.appendChild(c.file_info);
            c.cdn_wrap.appendChild(c.explorer);
            
            
            c.cdn_wrap.id = 'cdn_wrap';
            c.cdn_wrap.className = 'box';
            c.cdn_wrap.className = 'box';
            c.cdn_nav.id = 'cdn_nav';
            c.explorer.id = 'cdn_explorer';
            c.file_info.id = 'file_info';
            
            document.body.appendChild(c.cdn_wrap);
            
            c.proportion = 1;
            c.width = 160;
            c.height = 160;
            
            this._set_file_info();
            this._set_upload_img();
        }
        if ( ! c) {
            c = z._cdn;
        }
        this.cdn_wrap = c.cdn_wrap;
        
        this.cdn_nav = c.cdn_nav;
        this.cdn_path = c.cdn_path;
        this.input_new_folder = c.input_new_folder;
        this.bt_new_folder = c.bt_new_folder;
        this.explorer = c.explorer;
        this.file_info = c.file_info;
        
        this.input_width = c.input_width;
        this.input_height = c.input_height;
        this.input_src = c.input_src;
        //this.img_cdn_path = obj.cdn_path;
        
        this.proportion = c.proportion;
        this.width = c.width;
        this.height = c.height;

        
    };
    
    z.cdn.prototype = {
        set_pos: function (x, y) {
            if (x != 0) {
                this.cdn_wrap.style.left = x + 'px';
            }
            if (y != 0) {
                this.cdn_wrap.style.top = y + 'px';
            }
        },
        add_folder : function (folder_path) {
        
        },
        show: function (folder_path, callback) {
            folder_path = (folder_path == null)? this.current_path : folder_path;
            var requestUrl = z.site_url('?method=ajax&m=cdn&a=explorer&path=' + folder_path);
            var othis = this;
            this.cdn_path.innerHTML = '/' + folder_path;
            z.net.get(requestUrl, null, function (xhr) {
                //alert(xhr.responseText);
                othis.explorer.innerHTML = xhr.responseText;
            });
            this.current_path = folder_path;
            this.cdn_wrap.style.display = 'block';
            
            //this.callback = null;
            if (callback) {
                this.callback = callback;
            }
            
        },
        new_folder: function () {
            var new_folder_name = z.trim(this.input_new_folder.value);
            if (new_folder_name != '') {
                var folder_name = '';
                if (this.current_path == '') {
                    folder_name = new_folder_name;
                } else {
                    folder_name = this.current_path + '/' + new_folder_name;
                }
                
                var request_url = z.site_url('?method=ajax&m=cdn&a=new_folder&folder=' + folder_name);
                var othis = this;
                z.net.get(request_url, null, function (xhr) {
                    var result = xhr.responseText;
                    if (result == 'ok') {
                        othis.show(folder_name);
                        othis.input_new_folder.value = '';
                    } else {
                        alert(result);
                    }
                });
            }
        },
        upload_image: function () {
            
        },
        add_image : function (image_path, image_name) {
            
        },
        select_image: function (cdn_path) {
            var src = z.cdn_url(cdn_path);
            
            var obj = new Image();
            
            obj.src = src;
            obj.cdn_path = cdn_path;

            if (obj.complete) {
                //elem.src = obj.src;
                this._show_image_info(obj);
            } else {
                var othis = this;
                obj.onload = function () {
                    othis._show_image_info(this);
                    //delete this;
                };
            }
            
        },
        hide: function () {
            this.cdn_wrap.style.display = 'none';
        },
        _show_image_info: function (obj) {
            
            var elem = this.img_elem;
            elem.src = obj.src;
            if (obj.height > 160) {
                elem.height = 160;
                elem.width = 160 * obj.width / obj.height;
            } else {
                elem.height = obj.height;
                elem.width = obj.width;
            }
            if (elem.width > 160) {
                elem.width = 160;
                elem.height = 160 * obj.height / obj.width;
            }
            elem.style.left = (160 - elem.width) / 2 + 'px';
            elem.style.top = (160 - elem.height) / 2 + 'px';
            
            this.input_width.value = obj.width;
            this.input_height.value = obj.height;
            this.input_src.value = obj.cdn_path;
            this.img_cdn_path = obj.cdn_path;
            
            this.proportion = obj.width / obj.height;
            this.width = obj.width;
            this.height = obj.height;
            
            delete obj;
        },
        _set_file_info: function() {
            var othis = this;
            c = z._cdn;
            var file_info = c.file_info;
            var elem = document.createElement('img');
            this.img_elem = elem;
            
            var img_wrap = document.createElement('div');
            img_wrap.id = 'img_wrap';
            img_wrap.appendChild(elem);
            file_info.appendChild(img_wrap);
            
            var detail_wrap = document.createElement('div');
            file_info.appendChild(detail_wrap);
            
            var label_width = document.createElement('label');
            label_width.innerHTML = 'Width: ';
            detail_wrap.appendChild(label_width);
            
            var input_width = document.createElement('input');
            input_width.type = "text";
            //input_width.setAttribute("type", "text");
            input_width.className = "text";
            detail_wrap.appendChild(input_width);
            
            var label_height = document.createElement('label');
            label_height.innerHTML = 'Height:';
            detail_wrap.appendChild(label_height);
            
            var input_height = document.createElement('input');
            input_height.type = "text";
            input_height.className = "text";
            detail_wrap.appendChild(input_height);
            
            
            var label_src = document.createElement('label');
            label_src.innerHTML = 'Src:';
            detail_wrap.appendChild(label_src);
            
            var input_src = document.createElement('input');
            input_src.type = "text";
            input_src.className = "text";
            detail_wrap.appendChild(input_src);
            
            var bt_insert = document.createElement("a");
            bt_insert.href = "javascript:;";
            bt_insert.innerHTML = 'Insert';
            bt_insert.className = 'bt';
            detail_wrap.appendChild(bt_insert);
            bt_insert.onclick = function () {
                if (othis.callback && z.trim(othis.input_src.value) != '') {
                    othis.callback.call(othis);
                }
            };
            
            var bt_cancel = document.createElement("a");
            bt_cancel.href = "javascript:;";
            bt_cancel.innerHTML = 'Cancel';
            bt_cancel.className = 'bt';
            detail_wrap.appendChild(bt_cancel);
            
            bt_cancel.onclick = function () {
                othis.cdn_wrap.style.display = 'none';
            };
            
            c.input_width = input_width;
            c.input_height = input_height;
            c.input_src = input_src;
            c.bt_insert = bt_insert;
            
            
            c.input_width.onkeyup = function() {
                var width = parseInt(this.value);
                
                if (isNaN(width) || width <= 0) {
                    width = othis.width;
                } 
                othis.input_height.value = width * othis.height / othis.width;
                this.value = width;
            };
            c.input_height.onkeyup = function() {
                var height = parseInt(this.value);
                
                if (isNaN(height) || height <= 0) {
                    height = othis.height;
                }
                othis.input_width.value = height * othis.width / othis.height;
                this.value = height;
            };
        },
        _set_upload_img: function () {
            var uploadForm = document.createElement('form');
            uploadForm.setAttribute("target","ajax-temp-img-upload");
            uploadForm.setAttribute("action",z.site_url('?method=ajax&m=cdn&a=upload_img'));
            uploadForm.setAttribute("method","post");
            uploadForm.setAttribute("enctype","multipart/form-data");
            uploadForm.setAttribute("encoding","multipart/form-data");
            //uploadForm.setAttribute("style","position: absolute; top: 50px; left: 100px; background:#fff; border: 1px solid #E8E8E8; width: 500px;");
            uploadForm.id = 'upload_img_form';
            
            var input_path = document.createElement('input');
            input_path.setAttribute('type', 'hidden');
            input_path.setAttribute('name', 'path');
            uploadForm.appendChild(input_path);
            
            var input_file = document.createElement('input');
            input_file.setAttribute("type", "file");
            input_file.setAttribute("name", "img_input_file");
            input_file.id = 'img_input_file';
            input_file.className = 'file';
            
            var othis = this;
            
            input_file.onchange = function () {
                var form = this.form;
                form['path'].value = othis.current_path;
                form.submit();
                //input_file.form.submit();
            };
            
            var iframe = document.createElement("iframe");
            iframe.setAttribute("id","ajax-temp-img-upload");
            iframe.setAttribute("name","ajax-temp-img-upload");
            iframe.setAttribute("width","0");
            iframe.setAttribute("height","0");
            iframe.setAttribute("border","0");
            iframe.setAttribute("style","width: 0; height: 0; border: none;");
            
            z.addEvent(iframe,"load", function () {
                var iframe = z.s('ajax-temp-img-upload');
                var iframeDoc = iframe.contentDocument || iframe.contentWindow.document;
                var result = iframeDoc.body.innerHTML;
                if (result == 'ok') {
                    othis.show();
                    //z.s('upload_img').src = src;
                }
            });
            
            document.body.appendChild(iframe);
            
            uploadForm.appendChild(input_file);
            c.cdn_wrap.appendChild(uploadForm);
        }
    };
})();
